<template>
<div class="add_model_value_box">
    <div class="titl">
        是否有权限
    </div>
    <div class="lin">
        <div class="lin_item">
            <label v-for="(item, index) in store.state.modelList" :key="index">
                <input type="checkbox" :value="item.id" v-model="module_ids">
            {{ item.name }}
                <br v-if="(index + 1) % 3 == 0"/>
            </label>
        </div>
    </div>
    <div class="lin_btn">
        <button @click="addModelValue">
            保存
        </button>
    </div>
</div>
</template>

<script setup>
import {useRoute, useRouter} from 'vue-router'
import {getModeListReq, addModelValueReq} from '../api/index'
import {ref, onMounted} from 'vue'
import {useStore} from 'vuex'

const store = useStore()
const router = useRouter()
const route = useRoute()
const module_ids = ref([])
onMounted(() => {
    let user_id = route.query.id;
    getModeListReq(store.state.user.token, user_id).then((resp) => {
        if (resp.data.code == 200) {
            let data = resp.data.resp_data.module_list;
            for (let i = 0; i < data; i++) {
                module_ids.value.push(data[i].id)
            }
            console.log('module_ids', module_ids.value);
        }else {
            alert(resp.data.code_msg)
        }
    }).catch(err=> {
        console.log(err);
    })
})
const addModelValue = () => {
    let user_id = parseInt(route.query.id);
    console.log('id', user_id);
    console.log('module_List', store.state.modelList);
    console.log('module_ids', module_ids.value);
    addModelValueReq(store.state.user.token, {
        user_id, module_ids: module_ids.value
    }).then((resp) => {
        if (resp.data.code == 200) {
            alert('修改成功')
            router.push({
                path:'/main/userList'
            })
        }else {
            alert(resp.data.code_msg);
        }
    }).catch(err=> {
        console.log(err);
    })
}
</script>

<style scoped>
.add_model_value_box{

}
</style>